﻿/***********************************************************************
 * CLR 版本: 4.0.30319.225
 * 工程名称: $projectname$
 * 命名空间: Toulr.Test.Services
 * 文件名称: ShopServiceFixture
 * 描    述: 提供店铺信息管理 维护功能测试用例
 * 作    者: 18901309129@189.cn
 * 登录用户: hyyu
 * 创建时间: 2011-5-28 15:54:25
 * 创建年份: 2011
 * Update History: 
 ***********************************************************************/

using System;
using System.Collections.Generic;
using NUnit.Framework;
using Toulr.Domains.Business;
using Toulr.Domains.System;
using Toulr.Services;
using Toulr.Util.Ioc;

namespace Toulr.Test.Services
{
    /// <summary>
    /// ShopServiceFixture 的摘要说明。
    /// </summary>
    [TestFixture]
    public class ShopServiceFixture
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            //从容器内初始化接口的实例
            _shopService = UnityHelper.GetInstance<IShopService>();
        }

        #endregion

        private IShopService _shopService;

        /// <summary>
        /// 测试添加店铺列表
        /// </summary>
        [Test]
        public void AddShopTest()
        {
            //添加前先删除店铺

            var shops = _shopService.GetShops();
            foreach (var s in shops.Result)
            {
                _shopService.DeleteShop(s);
            }


            //只有地址
            Shop shop = new Shop()
            {
                Address = "中南海紫光阁",
            };
            var result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃一片顶过去五片，一口气上五楼不费劲"
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃一片顶过去五片，一口气上五楼不费劲",
                Features = "应有尽有。。。啥都有"
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃一片顶过去五片，一口气上五楼不费劲",
                Features = "应有尽有。。。啥都有",
                JoinTime = DateTime.Now
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃一片顶过去五片，一口气上五楼不费劲",
                Features = "应有尽有。。。啥都有",
                JoinTime = DateTime.Now,
                ShopCategoryId = 1
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃一片顶过去五片，一口气上五楼不费劲",
                Features = "应有尽有。。。啥都有",
                JoinTime = DateTime.Now,
                ShopCategoryId = 1,
                ShopKeeperId = 1
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃一片顶过去五片，一口气上五楼不费劲",
                Features = "应有尽有。。。啥都有",
                JoinTime = DateTime.Now,
                ShopCategoryId = 1,
                ShopKeeperId = 1,
                ShopManagerId = 2,
            };
            result = _shopService.AddShop(shop);
            Assert.AreNotEqual(0, result.ResultCode);
            Assert.IsNull(result.Result);

            shop = new Shop()
            {
                Address = "中南海紫光阁",
                BizLicense = "2306012345678",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃一片顶过去五片，一口气上五楼不费劲",
                Features = "应有尽有。。。啥都有",
                JoinTime = DateTime.Now,
                ShopCategoryId = 1,
                ShopKeeperId = 1,
                ShopManagerId = 2,
                //LicenseCommitTime = DateTime.Now,
                ShopName = "天上人间"
            };
            result = _shopService.AddShop(shop);
            Assert.AreEqual(0, result.ResultCode);


            shop = new Shop()
            {
                Address = "上地五街188号",
                BizLicense = "23060120998878",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "NDY",
                Features = "TDY",
                JoinTime = DateTime.Now,
                ShopCategoryId = 1,
                ShopKeeperId = 1,
                ShopManagerId = 2,
                LicenseCommitTime = DateTime.Now,
                ShopName = "WDY",
                ShoppingAreaId = 2
            };
            result = _shopService.AddShop(shop);
            Assert.GreaterOrEqual(0, result.ResultCode);

            shop = new Shop()
            {
                Address = "三里屯四里村",
                BizLicense = "2306087654321",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "名品折扣",
                Features = "便宜",
                JoinTime = DateTime.Now,
                ShopCategoryId = 1,
                ShopKeeperId = 1,
                ShopManagerId = 2,
                LicenseCommitTime = DateTime.Now,
                ShopName = "偷乐名品折扣",
                ShoppingAreaId = 2
            };
            result = _shopService.AddShop(shop);
            Assert.GreaterOrEqual(0, result.ResultCode);

            shop = new Shop()
            {
                Address = "北三环中路27号",
                BizLicense = "23060876543111",
                Certified = true,
                CertifiedTime = DateTime.Now,
                CityId = 101,
                CountyId = 10103,
                Description = "吃吃喝喝",
                Features = "人间美味",
                JoinTime = DateTime.Now,
                ShopCategoryId = 6,
                ShopKeeperId = 1,
                ShopManagerId = 2,
                LicenseCommitTime = DateTime.Now,
                ShopName = "成都小吃",
                ShoppingAreaId = 2
            };
            result = _shopService.AddShop(shop);
            Assert.GreaterOrEqual(0, result.ResultCode);
        }


        /// <summary>
        /// 测试获取店铺列表
        /// </summary>
        [Test]
        public void GetShopsTest()
        {
            var result = _shopService.GetShops();
            Assert.GreaterOrEqual(0,result.ResultCode);
            Assert.IsNotNull(result.Result);
            Assert.AreNotEqual(0, result.Result.Count);
            foreach (var shop in result.Result)
            {
                var result1 = _shopService.GetShops(shop);
                Assert.GreaterOrEqual(0, result.ResultCode);
                Assert.IsNotNull(result.Result);
                Assert.AreNotEqual(0, result.Result.Count);
            }
            Assert.Pass("OK");
        }

        [Test]
        public void GetOptionalPropertyTest()
        {
            var shop = new Shop{ShopCategoryId = 6};
            
            var result = _shopService.GetOptionalPropertys(null);
            Assert.AreEqual(1, result.ResultCode);

            result = _shopService.GetOptionalPropertys(shop);
            Assert.AreEqual(0,result.ResultCode);

            result = _shopService.GetOptionalPropertys(6);
            Assert.AreEqual(0,result.ResultCode);

            result = _shopService.GetOptionalPropertys(1234);
            Assert.AreEqual(0,result.Result.Count);

        }

        /// <summary>
        /// 测试添加店铺分类扩展属性
        /// </summary>
        [Test]
        public void AddShopPropertyTest()
        {
            var propertys = _shopService.GetOptionalPropertys(_shopService.GetShops(new Shop(){Id=1}).Result[0]);
            for (int i = 0; i < propertys.Result.Count; i++)
            {
                var cid = propertys.Result[i].Id;
                var cName = propertys.Result[i].PropName;
                var result =
                    _shopService.AddShopProperty(new ShopProperty()
                                                     {
                                                         PropertyId = cid,
                                                         PropertyName = cName,
                                                         PropertyValue = "刷卡",
                                                         ShopId = 1
                                                     });
                Assert.AreEqual(0,result.ResultCode);
            }
        }

        /// <summary>
        /// 测试获取店铺已有的分类扩展属性ID
        /// </summary>
        [Test]
        public void GetShopPropertysTest()
        {
            var result = _shopService.GetShopPropertys(1);
            Assert.AreEqual(0,result.ResultCode);
        }

        /// <summary>
        /// 测试更新店铺扩展属性
        /// </summary>
        [Test]
        public void UpdateShopPropertyTest()
        {
            var result = _shopService.UpdateShopProperty(new ShopProperty());
            Assert.AreEqual(1, result.ResultCode);
            result = _shopService.UpdateShopProperty(new ShopProperty(){Id=12345});
            Assert.AreEqual(1, result.ResultCode);

            var shopProperty = _shopService.GetShopPropertys(4);

            shopProperty = _shopService.GetShopPropertys(1);

            shopProperty.Result[0].PropertyValue = "刷卡";
            result = _shopService.UpdateShopProperty(shopProperty.Result[0]);
            Assert.AreEqual(0, result.ResultCode);

        }

        /// <summary>
        /// 测试获取店铺汇总评分
        /// </summary>
        [Test]
        public void GetShopEvaluationTest()
        {
            var result = _shopService.GetShopEvaluation(12345);
            Assert.AreEqual(1,result.ResultCode);
            result = _shopService.GetShopEvaluation(new Shop(){Id=12345});
            Assert.AreEqual(1, result.ResultCode);
            result = _shopService.GetShopEvaluation(4);
            Assert.AreEqual(0, result.ResultCode);
            result = _shopService.GetShopEvaluation(new Shop(){Id=4});
            Assert.AreEqual(0, result.ResultCode);
        }

        /// <summary>
        /// 测试获取店铺评论列表
        /// </summary>
        [Test]
        public void GetShopEvaluationDetailsTest()
        {
            var result = _shopService.GetShopEvaluationDetails(123);
            Assert.AreEqual(0,result.Result.Count);
            result = _shopService.GetShopEvaluationDetails(new Shop());
            Assert.IsNotNull(result.Result);
            result = _shopService.GetShopEvaluationDetails(new Shop(){Id=4});
            Assert.AreEqual(0, result.ResultCode);
            result = _shopService.GetShopEvaluationDetails(4);
            Assert.AreEqual(0, result.ResultCode);
        }

        /// <summary>
        /// 测试添加店铺的评论
        /// </summary>
        [Test]
        public void AddShopEvaluationDetailTest()
        {
            var optionalEvaluation = _shopService.GetOptionalEvaluation(new Shop() { Id = 4 });
            Assert.AreEqual(0, optionalEvaluation.ResultCode);

            var detail = new ShopEvaluationDetail();
            detail.UserId = 1;
            detail.EvaluationTime = DateTime.Now;
            detail.EvaluationContext = "55555";
            detail.AbetCount = 0;
            detail.ShopId = 4;
            int index = 1;
            Random random=new Random();
            foreach (InfoCategoriesProperty property in optionalEvaluation.Result)
            {
                detail.GetType().GetProperty("ItemName"+index).SetValue(detail,property.PropName,null);
                detail.GetType().GetProperty("ItemValue" + index).SetValue(detail,Convert.ToSingle(Math.Round(random.NextDouble(),3))*10, null);
                index++;
            }
            _shopService.AddShopEvaluationDetail(detail);

        }

        /// <summary>
        /// 测试修改店铺评论
        /// </summary>
        [Test]
        public void UpdateShopEvaluationDetailTest()
        {
            var shopEvaluationDetails= _shopService.GetShopEvaluationDetails(4);
            Assert.AreEqual(0,shopEvaluationDetails.ResultCode);
            shopEvaluationDetails = _shopService.GetShopEvaluationDetails(new Shop() {Id = 0});
            Assert.AreNotEqual(0,shopEvaluationDetails.ResultCode);
            shopEvaluationDetails = _shopService.GetShopEvaluationDetails(12);
            Assert.AreEqual(0, shopEvaluationDetails.Result.Count);
            shopEvaluationDetails = _shopService.GetShopEvaluationDetails(new Shop() { Id = 4 });
            Assert.AreEqual(0, shopEvaluationDetails.ResultCode);
            shopEvaluationDetails.Result[0].EvaluationContext = "谁说有蟑螂？";
            shopEvaluationDetails.Result[0].ItemValue1 = 5;
            var result = _shopService.UpdateShopEvaluationDetail(shopEvaluationDetails.Result[0]);
            Assert.AreEqual(0,result.ResultCode);
        }

        /// <summary>
        /// 测试删除店铺评论
        /// </summary>
        [Test]
        public void DeleteShopEvaluationDetailTest()
        {
            var result= _shopService.DeleteShopEvaluationDetail(12);
            Assert.AreNotEqual(0,result.ResultCode);
            result = _shopService.DeleteShopEvaluationDetail(new ShopEvaluationDetail(){Id = 123});
            Assert.AreNotEqual(0, result.ResultCode);
            result = _shopService.DeleteShopEvaluationDetail(new ShopEvaluationDetail(){Id = 1});
            Assert.AreEqual(0, result.ResultCode);
            result = _shopService.DeleteShopEvaluationDetail(2);
            Assert.AreEqual(0, result.ResultCode);
        }

        [Test]
        public void GetOptionalEvaluationTest()
        {
            var result = _shopService.GetOptionalEvaluation(new Shop() {Id = 4});
            Assert.AreEqual(0,result.ResultCode);
        }

        [Test]
        public void AddEvaluationReplyTest()
        {
            var result = _shopService.AddEvaluationReply(null);
            Assert.AreNotEqual(0,result.ResultCode);

            result = _shopService.AddEvaluationReply(new EvaluationReply(){ReplyContext = "瞎说，没有蟑螂",ShopEvaluationDetailId = 3,ShopId = 4,UserId = 1});
            Assert.AreEqual(0, result.ResultCode);

            result = _shopService.AddEvaluationReply(new EvaluationReply() { ReplyContext = "瞎说，没有蟑螂", ShopEvaluationDetailId = 18, ShopId = 4, UserId = 1 });
            Assert.AreNotEqual(0, result.ResultCode);

            result = _shopService.AddEvaluationReply(new EvaluationReply() { ReplyContext = "瞎说，没有蟑螂", ShopEvaluationDetailId = 3, ShopId = 15, UserId = 1 });
            Assert.AreEqual(0, result.ResultCode);

            result = _shopService.AddEvaluationReply(new EvaluationReply() { ReplyContext = "瞎说，没有蟑螂", ShopEvaluationDetailId = 3, ShopId = 4, UserId = 1111 });
            Assert.AreNotEqual(0, result.ResultCode);

            result = _shopService.AddEvaluationReply(new EvaluationReply() { ReplyContext = "真的有有蟑螂，好大", ShopEvaluationDetailId = 3, ShopId = 4, UserId = 1 });
            Assert.AreEqual(0, result.ResultCode);
        }


        [Test]
        public void GetShopShareInCountTest()
        {
            var result = _shopService.GetShopShareInCount(123);
            Assert.AreNotEqual(0,result.ResultCode);

            result = _shopService.GetShopShareInCount(new Shop());
            Assert.AreNotEqual(0, result.ResultCode);

            result = _shopService.GetShopShareInCount(new Shop(){Id=4});
            Assert.AreEqual(0, result.ResultCode);
        }

        [Test]
        public void GetAboutHotShopsTest()
        {
            var result = _shopService.GetAboutHotShops(1, 50000, 10);
            Assert.AreEqual(0,result.ResultCode);

            result = _shopService.GetAboutHotShops(1, 5000, 10);
            Assert.AreEqual(0, result.ResultCode);

            result = _shopService.GetAboutHotShops(1, 1000, 10);
            Assert.AreEqual(0, result.ResultCode);

            result = _shopService.GetAboutHotShops(1, 10, 10);
            Assert.AreEqual(0, result.ResultCode);

            result = _shopService.GetAboutHotShops(1, 100, 10);
            Assert.AreEqual(0, result.ResultCode);

            result = _shopService.GetAboutHotShops(1, 500, 10);
            Assert.AreEqual(0, result.ResultCode);


        }
    }
}
